/*
 * Created by SharpDevelop.
 * User: Tivadar Papai
 * Date: 8/30/2007
 * Time: 11:21 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

using RoverStudio.Common.WorldModel;
using RoverStudio.Math;
using CaterpillarRover;

namespace RoverStudio.MapDisplay.WM2D
{

	/// <summary>
	/// Description of MapControlUC.
	/// </summary>
	public partial class MapControlUC : UserControl
	{
        private List<MapPlugin> plugins = new List<MapPlugin>();
        private WorldModel worldModel;

        public MapDisplay2DControl MapDisplay
        {
            get { return mapDisplay2DControl; }
        }

        public WorldModel WorldModel
        {
            get
            {
                return this.worldModel;
            }
            set
            {
                this.worldModel = value;
                this.MapDisplay.WorldModel = worldModel;

                CaterpillarVehicle rover1 = (CaterpillarVehicle)worldModel.FindObject("Rover 1");
                CaterpillarVehicle rover2 = (CaterpillarVehicle)worldModel.FindObject("Rover 2");
                PathMPUC p1 = new PathMPUC();
                p1.WorldModel = worldModel;
                p1.Rover = rover1;
                AddMapControl(p1);

                PathMPUC p2 = new PathMPUC();
                p2.WorldModel = worldModel;
                p2.Rover = rover2;
                AddMapControl(p2);



                this.MapDisplay.ObjectsProperties.Add(rover1, Color.Red);
                this.MapDisplay.ObjectsProperties.Add(rover2, Color.Green);

                
            }
        }

        public ObjectFollowerControllerUC ObjectFollowerControllerUC
        {
            get
            {
                return objectFollowerControllerUC1;
            }

        }

		public MapControlUC()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();

			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
            this.mapDisplay2DControl.Origin = new Vector2D((double)this.mapDisplay2DControl.Width / 2.0, (double)this.mapDisplay2DControl.Height / 2.0);


            GeneralMPUC generalMPUC = new GeneralMPUC();
            generalMPUC.MapDisplay = this.mapDisplay2DControl;
            plugins.Add(generalMPUC);

            lbMapControls.DataSource = plugins;
            lbMapControls.DisplayMember = "Description";

            generalMPUC.SetZoom(1);

            //TEST_START
            //WorldModel wm = new WorldModel();
            //wm.AddWorldModelObject(new PhysicalObject(wm, new Vector3D(0.0, 10.0, 0.0), new Vector3D(0.1, 0.1, 0.1),
            //    new Rotation(new double[,] { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } })));
            //this.MapDisplay.WorldModel = wm;


            //CaterpillarVehicle rover1 = new CaterpillarVehicle(wm, "Rover 1", new Vector3D(0.0, 20.0, 0.0), new Vector3D(1.1, 0.1, 0.1),
            //    new Rotation(new double[,] { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } }));

            //CaterpillarVehicle rover2 = new CaterpillarVehicle(wm, "Rover 2", new Vector3D(40.0, 10.0, 0.0), new Vector3D(1.1, 0.1, 0.1),
            //    new Rotation(new double[,] { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } }));



            //PathMPUC p1 = new PathMPUC();
            //p1.WorldModel = wm;
            //p1.Rover = rover1;
            //AddMapControl(p1);
  
            //PathMPUC p2 = new PathMPUC();
            //p2.WorldModel = wm;
            //p2.Rover = rover2;
            //AddMapControl(p2);

            this.MapDisplay.ObjectsProperties.Add("Background color", Color.Gray);
            //this.MapDisplay.ObjectsProperties.Add(rover1, Color.Red);
            //this.MapDisplay.ObjectsProperties.Add(rover2, Color.Green);
          

            //TEST_END
		}



        public void AddMapControl(MapPlugin plugin)
        {
            plugins.Add(plugin);
            ((CurrencyManager)BindingContext[plugins]).Refresh();
        }

        public void RemoveMapControl(MapPlugin plugin)
        {
            plugins.Remove(plugin);
            ((CurrencyManager)BindingContext[plugins]).Refresh();
        }

        private MapPlugin currentPlugin = null;
        private void lbMapControls_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlCurrentControl.Controls.Clear();
            if (currentPlugin !=null)
                mapDisplay2DControl.MapUserInteraction -=
                    new MapUserInteractionEventHandler(currentPlugin.HandleMapInteraction);
            currentPlugin = (MapPlugin)lbMapControls.SelectedItem;
            if (currentPlugin != null)
                mapDisplay2DControl.MapUserInteraction +=
                    new MapUserInteractionEventHandler(currentPlugin.HandleMapInteraction);
            pnlCurrentControl.Controls.Add((Control)lbMapControls.SelectedItem);
        }
	}
}
